﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using tmAsk.Common;

namespace tmAsk.Member
{
    public partial class Message : System.Web.UI.Page
    {

        tmAsk.Data.MakeHtml make = new tmAsk.Data.MakeHtml();
        tmAsk.Data.MakehtmlSQL SQL = new tmAsk.Data.MakehtmlSQL();

        protected void Page_Load(object sender, EventArgs e)
        {
            function.CheckUserLogin();
            string uid = function.Getcookies();
            int pageindex = 1;  //页码
            int page = 6;       //每页条数
            int pagecount = 1;  //总页数
            int count = 0;        //共多少条

            if (!string.IsNullOrEmpty(Request.QueryString["page"]))
            {
                pageindex = Convert.ToInt32(Request.QueryString["page"]);
            }

            DataRow dr = SQL.GetList("b_user", "u_id='" + uid + "'", "u_id").Tables[0].Rows[0];
            //string sql = "select * from (select top " + page + " * from (select top " + pageindex * page + " * from b_msg where m_acceptuser=" + dr["u_id"].ToString() + " order by m_id desc) order by m_id asc) order by m_id desc";
            DataSet ds = SQL.GetList("b_msg", "m_id", page, pageindex, "m_id desc", 1, "m_acceptuser=" + dr["u_id"].ToString());
            rptList.DataSource = ds;
            rptList.DataBind();

            DataSet ds2 = SQL.GetList("b_msg", "m_acceptuser=" + dr["u_id"].ToString(), "m_id");

            if ((ds2.Tables[0].Rows.Count % page) > 0)
            {
                pagecount = ds2.Tables[0].Rows.Count / page + 1;
            }
            else
            {
                pagecount = ds2.Tables[0].Rows.Count / page;
            }

            count = ds2.Tables[0].Rows.Count;

            lblTxt.Text = make.htmlpage(count, page, pageindex, "/member/message.aspx?", "page=");
        }

        public string GetMsgType(object obj, object obj2)
        {
            string title = string.Empty;
            if (obj.ToString() == "0")
            {
                title = "管理员";
            }
            else
            {
                title = "系统";
            }

            if (obj2.ToString() == "0")
            {
                title = "<b>" + title + "</b>";
            }
            return title;
        }

        public string GetState(object obj)
        {
            if (obj.ToString() == "0")
            {
                return "<b>未读</b>";
            }

            return "已读";
        }
        protected void btnDel_Click(object sender, EventArgs e)
        {
            string str = Request.Form["messDel"];
            if (str == null || str == "")
            {
                Response.Write(function.alert_text(4, "尚未选择数据", ""));
                return;
            }

            string[] id = str.Split(',');
            foreach (string item in id)
            {
                string sql = "delete from B_msg where m_id=" + item;
                SQL.ExecuteSql(sql);
            }
            Response.Write(function.alert_text(2, "删除成功", "message.aspx"));
        }
    }
}